package com.biostime.dingdingbox.modules.bt.web;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.biostime.dingdingbox.common.config.Global;
import com.biostime.dingdingbox.common.persistence.Page;
import com.biostime.dingdingbox.common.utils.StringUtils;
import com.biostime.dingdingbox.common.web.BaseController;
import com.biostime.dingdingbox.modules.bt.entity.Project;
import com.biostime.dingdingbox.modules.bt.service.ProjectService;

@Controller
@RequestMapping(value = "${adminPath}/bt/project")
public class ProjectContoller extends BaseController {

	@Autowired
	private ProjectService proService;

	@ModelAttribute
	public Project get(@RequestParam(required = false) String id) {
		Project entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = proService.get(id);
		}
		if (entity == null) {
			entity = new Project();
		}
		return entity;
	}

	@RequestMapping(value = "view")
	public String view(Project project, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<Project> page = proService.findPage(new Page<Project>(request,
				response), project);
		model.addAttribute("page", page);
		return "modules/bt/projectList";
	}

	@RequestMapping(value = "form")
	public String form(Project project, Model model) {
		model.addAttribute("project", project);
		return "modules/bt/projectForm";
	}

	@RequestMapping(value = "save")
	public String save(Project project) {
		proService.save(project);
		return "redirect:" + Global.getAdminPath() + "/bt/project/view";
	}

	@RequestMapping(value = "delete")
	public String delete(
			@RequestParam(value = "ids", required = false) String idsRequest,
			Project project) {
		if (!StringUtils.isEmpty(idsRequest)) {
			String[] ids = idsRequest.split(",");
			proService.delete(ids);
		} else {
			String[] ids = {project.getId()};
			proService.delete(ids);;
		}
		return "redirect:" + Global.getAdminPath() + "/bt/project/view";
	}
	

}
